package com.ruoyi.fun.controller;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.fun.domain.EcSalesReport;
import com.ruoyi.fun.service.IEcSalesReportService;

/**
 * 商品销售报表Controller
 * 
 * @author ruoyi
 * @date 2025-01-17
 */
@Anonymous
@RestController
@RequestMapping("/statistics/good_analyse/sale")
public class EcSalesReportController extends BaseController
{
    @Autowired
    private IEcSalesReportService ecSalesReportService;

    /**
     * 查询商品销售报表列表
     */
    @GetMapping("")
    public TableDataInfo list(
            @RequestParam(value = "productName", required = false) String productName,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize)
    {
        startPage();
        
        Map<String, Object> params = new HashMap<>();
        params.put("productName", productName);
        params.put("categoryId", categoryId);
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        // 添加组织ID筛选，根据登录用户的DeptId
        params.put("orgId", getDeptId());
        
        List<EcSalesReport> list = ecSalesReportService.selectSalesReportList(params);
        return getDataTable(list);
    }

    /**
     * 根据商品ID查询销售报表详情
     */
    @GetMapping("/detail/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        EcSalesReport report = ecSalesReportService.selectSalesReportByProductId(productId);
        return success(report);
    }

    /**
     * 根据分类ID查询销售报表汇总
     */
    @GetMapping("/category/{categoryId}")
    public AjaxResult getByCategoryId(@PathVariable("categoryId") Long categoryId)
    {
        List<EcSalesReport> list = ecSalesReportService.selectSalesReportByCategoryId(categoryId);
        return success(list);
    }

    /**
     * 查询销售报表统计数据
     */
    @GetMapping("/statistics")
    public AjaxResult getStatistics(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate)
    {
        Map<String, Object> params = new HashMap<>();
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        // 添加组织ID筛选，根据登录用户的DeptId
        params.put("orgId", getDeptId());
        
        Map<String, Object> statistics = ecSalesReportService.selectSalesReportStatistics(params);
        return success(statistics);
    }

    /**
     * 导出商品销售报表列表
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response,
            @RequestParam(value = "productName", required = false) String productName,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate)
    {
        Map<String, Object> params = new HashMap<>();
        params.put("productName", productName);
        params.put("categoryId", categoryId);
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        // 添加组织ID筛选，根据登录用户的DeptId
        params.put("orgId", getDeptId());
        
        List<EcSalesReport> list = ecSalesReportService.exportSalesReportList(params);
        ExcelUtil<EcSalesReport> util = new ExcelUtil<EcSalesReport>(EcSalesReport.class);
        util.exportExcel(response, list, "商品销售报表数据");
    }
}